<template>
	<view class="category-container">
		<!-- 主体 -->
		<view class="category-body">
			<!-- 左侧分类导航 -->
			<scroll-view scroll-y class="left-nav" :style="{height: scrollHeight + 'px'}">
				<view v-for="(item, index) in categories" :key="item.id" class="nav-item"
					:class="{active: currentIndex === index}" @click="changeCategory(index)">
					{{item.name}}
				</view>
			</scroll-view>
			<!-- 右侧商品列表 -->
			<scroll-view scroll-y class="right-content" :style="{height: scrollHeight + 'px'}" enable-flex
				:scroll-top="scrollTop">
				<!-- 大分类 -->
				<image v-if="currentCategoryData.banner" :src="currentCategoryData.banner" mode="widthFix"
					class="category-banner"></image>
				<!-- 子分类 -->
				<view v-for="subCat in currentCategoryData.children" :key="subCat.id" class="sub-category"
					:id="'sub_' + subCat.id">
					<view class="sub-title">
						<text class="name">{{subCat.name}}</text>
						<text class="more" @click="goToSubCategory(subCat)">更多 ></text>
					</view>
					<!-- 分类列表详情 -->
					<view class="goods-list">
						<view v-for="goods in subCat.goodsList" :key="goods.id" class="goods-item"
							@click="goToSubCategory(goods)">
							<image :src="goods.image" mode="aspectFill" class="goods-image"></image>
							<view class="goods-info">
								<view class="goods-name u-line-2">{{goods.name}}</view>
							</view>
						</view>
					</view>
				</view>
			</scroll-view>
		</view>
	</view>
</template>

<script>
	import {
		httpsend
	} from '@/api/api';
	export default {
		data() {
			return {
				scrollTop: 0, //滚动条置顶用
				scrollHeight: 0, // 滚动区域高度
				currentIndex: 0, // 当前选中分类索引
				categories: [] //页面数据
			}
		},
		computed: {
			// 当前选中的分类数据
			currentCategoryData() {
				return this.categories[this.currentIndex] || {}
			},
			// 当前分类名称
			currentCategory() {
				return this.currentCategoryData.name || '商品分类'
			}
		},
		onLoad() {
			this.calcScrollHeight();
			this.loaddata();
		},
		methods: {
			//加载数据
			async loaddata() {
				uni.showLoading({
					title: '加载中...'
				});
				try {
					const res = await httpsend('getcategory');
					if (res?.code === 1) {
						this.categories = res.data;
					} else {
						this.$api.msg(res.msg);
					}
					uni.hideLoading();
				} catch (err) {
					console.log('err', err);
					uni.hideLoading();
				}
			},
			// 计算滚动区域高度
			calcScrollHeight() {
				const systemInfo = uni.getSystemInfoSync()
				this.scrollHeight = systemInfo.windowHeight // 减去导航栏高度
			},
			// 切换分类
			changeCategory(index) {
				//切换后滚动条置顶
				this.scrollTop = 0;
				// 小程序需要重置后才能生效
				this.$nextTick(() => {
					this.scrollTop = 1; // 稍微改变值确保触发
					this.$nextTick(() => {
						this.scrollTop = 0;
					});
				});
				this.currentIndex = index
			},

			// 跳转子分类页
			goToSubCategory(subCat) {
				uni.navigateTo({
					url: `/pages/goodsinfo/goodslist?id=${subCat.id}&name=${subCat.name}`
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	.category-container {
		display: flex;
		flex-direction: column;
		height: 100vh;
		background-color: #f7f7f7;
	}

	.category-body {
		display: flex;
		flex: 1;
		overflow: hidden;
	}

	/* 左侧导航 */
	.left-nav {
		width: 180rpx;
		background-color: #f8f8f8;

		.nav-item {
			height: 100rpx;
			display: flex;
			align-items: center;
			justify-content: center;
			font-size: 28rpx;
			color: #666;
			position: relative;

			&.active {
				color: #e74c3c;
				background-color: #fff;
				font-weight: bold;

				&::before {
					content: '';
					position: absolute;
					left: 0;
					top: 20rpx;
					bottom: 20rpx;
					width: 6rpx;
					background-color: #e74c3c;
					border-radius: 3rpx;
				}
			}
		}
	}

	/* 右侧内容 */
	.right-content {
		flex: 1;
		padding: 20rpx;
		box-sizing: border-box;

		.category-banner {
			width: 100%;
			border-radius: 12rpx;
			margin-bottom: 30rpx;
		}

		.sub-category {
			margin-bottom: 40rpx;

			.sub-title {
				display: flex;
				justify-content: space-between;
				align-items: center;
				margin-bottom: 20rpx;

				.name {
					font-size: 32rpx;
					font-weight: bold;
					color: #333;
				}

				.more {
					font-size: 24rpx;
					color: #999;
				}
			}
		}

		.goods-list {
			display: flex;
			flex-wrap: wrap;
			justify-content: space-between;

			.goods-item {
				width: 48%;
				margin-bottom: 20rpx;
				background-color: #fff;
				border-radius: 12rpx;
				overflow: hidden;

				.goods-image {
					width: 100%;
					height: 300rpx;
				}

				.goods-info {
					padding: 16rpx;

					.goods-name {
						font-size: 26rpx;
						color: #333;
						height: 72rpx;
						line-height: 36rpx;
						margin-bottom: 10rpx;
					}

					.goods-price {
						font-size: 30rpx;
						color: #e74c3c;
						font-weight: bold;
					}
				}
			}
		}
	}
</style>